<?php
/**
 * @return
 * @author 朱乐斌
 * @date
 */

namespace app\api\controller;

use app\api\model\EnterpriseModel;
use app\api\model\QualificationsModel;
use app\api\model\ReleasePositionModel;
use app\api\model\UserGxModel;
use app\api\validate\ReleasePositionValidate;
use think\Request;

class ReleasePosition extends BaseController
{


    // 登录
    public function login()
    {

    }



    // 发布职位
    public function releasePosition(Request $request)
    {
        //  接收数据
        $data = $this->request->post();
        try {
            // 判断用户类型
            $user = UserGxModel::where('phone',$request->phone)->find();
            if($user['type'] != 2) return errMsg('非企业用户');
            // 验证接收的数据
            validate(ReleasePositionValidate::class)->scene('releasePosition')->check($data);
            // 查询公司id
            $ent = (new \app\api\model\EnterpriseModel)->where('user_id',$user['id'])->select();
            if (empty($ent)) return  errMsg('请先创建公司');
            // 转化学历
            $qualifications = QualificationsModel::where('name',$data['qualifications'])->find();
            if (empty($qualifications)) return errMsg('请输入正确的学历');
            // 数据入库
            $date = [
                'enterprise_id' => $ent[0]['id'],
                'name' => $data['name'],
                'post_type_id' => $data['post_type_id'],
                'qualifications_id' => $qualifications['id'],
                'salary_min' => $data['salary_min'],
                'salary_max' => $data['salary_max'],
                'content' => $data['content'],
                'welfare_content' => $data['welfare_content'],
                'address' => $data['address']
            ];
            $res = new ReleasePositionModel();
            $res = $res->save($date);
            if ($res) return succMsg(array(),'发布成功');
        }catch (\Exception $e){
            return errMsg($e->getMessage());
        }

    }

}